package org.mappland.compltres.controller.user;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.mappland.compltres.common.CommonResult;
import org.mappland.compltres.common.StatusCode;
import org.mappland.compltres.domain.User;
import org.mappland.compltres.dto.LoginDTO;
import org.mappland.compltres.dto.PasswordDTO;
import org.mappland.compltres.dto.RegisterDTO;
import org.mappland.compltres.dto.ReturnUserDTO;
import org.mappland.compltres.service.UserService;
import org.mappland.compltres.util.WebUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    // 用户注册
    @PostMapping(value = "/register")
    public CommonResult register(@RequestBody RegisterDTO registerDTO) {
        // System.out.println(registerDTO.toString());
        long count = userService.count(new QueryWrapper<User>().eq("email", registerDTO.getEmail()));
        // 邮箱重复
        if (count > 0) {
            CommonResult<String> commonResult = new CommonResult<>();

            commonResult.setData("邮箱已经存在");
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            return commonResult;
        }

        // 正常注册
        CommonResult<String> commonResult = new CommonResult<>();
        User user = new User();
        BeanUtils.copyProperties(registerDTO, user);
        user.setPassword(SecureUtil.md5(registerDTO.getPassword()));
        userService.save(user);
        commonResult.setData("注册成功");
        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        return commonResult;
    }

    // 用户登录
    @PostMapping(value = "/login")
    public CommonResult<String> login(@RequestBody LoginDTO loginDTO) {
        CommonResult<String> commonResult = new CommonResult<>();
        // 邮箱验证
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("email", loginDTO.getEmail());
        // 密码验证
        String md5Password = SecureUtil.md5(loginDTO.getPassword());
        queryWrapper.eq("password", md5Password);
        User user = userService.getBaseMapper().selectOne(queryWrapper);

        if (null != user) {
            WebUtils.getSession().setAttribute("loginUser", user);
            commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
            commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
            commonResult.setData("登陆成功");
        } else {
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("密码错误，请重试");
        }

        return commonResult;
    }

    // 登出逻辑
    @GetMapping("/logout")
    public CommonResult<String> logout(){
        WebUtils.getSession().removeAttribute("loginUser");
        CommonResult<String> commonResult = new CommonResult<>();

        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        commonResult.setData("登出成功!");

        return commonResult;
    }

    // 获取用户信息
    @GetMapping("/userDetail")
    public CommonResult<ReturnUserDTO> userDetail() {
        CommonResult<ReturnUserDTO> commonResult = new CommonResult<>();
        ReturnUserDTO returnUserDTO = new ReturnUserDTO();

        User user2 = (User) WebUtils.getSession().getAttribute("loginUser");
        User user = userService.getBaseMapper().selectById(user2.getId());
        BeanUtils.copyProperties(user, returnUserDTO);

        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        commonResult.setData(returnUserDTO);

        return commonResult;
    }

    // 重置密码
    @PostMapping("/updatePassword")
    public CommonResult<String> updatePassword(@RequestBody PasswordDTO passwordDTO) {
        CommonResult<String> commonResult = new CommonResult<>();
        QueryWrapper queryWrapper = new QueryWrapper();

        User user2 = (User) WebUtils.getSession().getAttribute("loginUser");
//        System.out.println(user2.toString());

        User user = userService.getById(user2.getId());
//        System.out.println(user.toString());
//
//        System.out.println(passwordDTO.toString());

        String oldPassword_md5 = SecureUtil.md5(passwordDTO.getOldPassword());
//        System.out.println(oldPassword_md5);
//        System.out.println(user.getPassword());
//        System.out.println(user.getPassword().equals(oldPassword_md5));

        if(!user.getPassword().equals(oldPassword_md5)){
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("密码错误");

            return commonResult;
        }

        String newPassword_md5 = SecureUtil.md5(passwordDTO.getNewPassword());
        user.setPassword(newPassword_md5);
        userService.updateById(user);

        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        commonResult.setData("密码修改成功!");

        return commonResult;
    }
}
